package com.suiyun.EventBusDemo.sync.handler;

import com.google.common.eventbus.EventBus;
import com.suiyun.EventBusDemo.sync.event.DemoEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.concurrent.RejectedExecutionException;

/**
 * @author ww
 * @date 2020/10/29 上午10:05
 */
@Service
public class EventHandler {

    @Autowired
    private EventBus eventBus;

    @Autowired
    private EventListener eventListener;

    @PostConstruct
    public void init() {
        eventBus.register(eventListener);
    }

    @PreDestroy
    public void destroy() {
        eventBus.unregister(eventListener);
    }

    public void eventPost(DemoEvent event) throws RejectedExecutionException {
        eventBus.post(event);
    }

}
